#region License, Terms and Conditions
//
// Prawks - Copyright (c) 2007 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

namespace Prawks.CodeProvisioning
{
    using System;

    using TypeBuilder = System.Reflection.Emit.TypeBuilder;
    using MethodBuilder = System.Reflection.Emit.MethodBuilder;
    using MethodInfo = System.Reflection.MethodInfo;

    /// <summary>
    /// Allows a type to externally provide the implementation for a method.
    /// </summary>
    
    public interface IMethodImpl
    {
        /// <summary>
        /// Gets the method to which the implementation is bound.
        /// </summary>
        
        MethodInfo Method { get; }
    }

    /// <summary>
    /// Allows a type to compile the implementatin that it provides externally
    /// for a method.
    /// </summary>

    public interface IMethodImplCompiler
    {
        /// <summary>
        /// Creates and compiles an implementation of the bound method into a new type.
        /// </summary>
        /// <param name="targetType">
        /// The type on which to define the new method and compile the implementation.
        /// </param>
        /// <returns>
        /// Returns the compiled method.
        /// </returns>
        
        MethodBuilder Compile(TypeBuilder targetType);
    }

    /// <summary>
    /// When overriden in a derived class, allows a type to be designated as
    /// the external implementation provider for a method.
    /// </summary>

    [ AttributeUsage(AttributeTargets.Method) ]
    public abstract class MethodImplAttribute : MemberImplAttribute
    {
        /// <summary>
        /// Creates the implementation provider for the method to which the 
        /// attribute is applied.
        /// </summary>
        /// <param name="method">
        /// The method whose implementation provider is to be supplied.
        /// </param>
        /// <returns>
        /// Returns the implementation provider for the method.
        /// </returns>
        
        public virtual IMethodImpl CreateImpl(MethodInfo method)
        { 
            Type implType = InferImplType("Impl");
            
            if (implType == null)
            {
                throw new MemberImplException(
                    string.Format("{0} has no corresponding implementation provider type. Override and supply an implementation for CreateImpl on {0}.",
                    GetType().FullName));
            }

            return (IMethodImpl) Activator.CreateInstance(implType, new object[] { method });
        }
    }
}
